/*
 * Copyright (C) 2004, Velocityme Partnership
 * NodeUtil.java
 *
 * Created on 28 August 2004, 09:10
 */

package com.velocityme.www.utility;

import com.velocityme.entity.PermissionBean;
import com.velocityme.interfaces.KeySession;
import com.velocityme.interfaces.PermissionPK;
import com.velocityme.interfaces.RemoteClientSession;
import com.velocityme.interfaces.RemoteClientSessionUtil;
import com.velocityme.utility.InvalidKeyException;
import com.velocityme.valueobjects.*;
import com.velocityme.www.dto.ButtonValue;
import com.velocityme.www.dto.IntegerString;
import com.velocityme.www.dto.MenuDTO;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.ejb.CreateException;
import javax.naming.NamingException;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeModel;

/**
 *
 * @author  Robert
 */
public class NodeUtil {
    
    private static NodeUtil m_cachedNodeUtil = null;
    private Hashtable m_nodeTabs;
    private IntegerString[] m_months;
    private IntegerString[] m_days;
    private IntegerString[] m_hours;
    private IntegerString[] m_minutes;
    private IntegerString[] m_durationDays;
    private IntegerString[] m_durationHours;
    private PermissionPK permissionNodeCreate = new PermissionPK(new Integer(PermissionBean.NODE_CREATE));
    private PermissionPK permissionNodeEdit = new PermissionPK(new Integer(PermissionBean.NODE_EDIT));
    private PermissionPK permissionNodeDelete = new PermissionPK(new Integer(PermissionBean.NODE_DELETE));
    private PermissionPK permissionNodePostComment = new PermissionPK(new Integer(PermissionBean.NODE_POST_COMMENT));
    private PermissionPK permissionTaskChangeState = new PermissionPK(new Integer(PermissionBean.TASK_CHANGE_STATE));
    private PermissionPK permissionProjectCreate = new PermissionPK(new Integer(PermissionBean.PROJECT_CREATE));
    private PermissionPK permissionProjectEdit = new PermissionPK(new Integer(PermissionBean.PROJECT_EDIT));
    private PermissionPK permissionAnchorCreate = new PermissionPK(new Integer(PermissionBean.ANCHOR_CREATE));
    private PermissionPK permissionAnchorEdit = new PermissionPK(new Integer(PermissionBean.ANCHOR_EDIT));
    private PermissionPK permissionActionCreate = new PermissionPK(new Integer(PermissionBean.ACTION_CREATE));
    private PermissionPK permissionActionEdit = new PermissionPK(new Integer(PermissionBean.ACTION_EDIT));
    private PermissionPK permissionIssueCreate = new PermissionPK(new Integer(PermissionBean.ISSUE_CREATE));
    private PermissionPK permissionIssueEdit = new PermissionPK(new Integer(PermissionBean.ISSUE_EDIT));
    private PermissionPK permissionOrganisationCreate = new PermissionPK(new Integer(PermissionBean.ORGANISATION_CREATE));
    private PermissionPK permissionOrganisationEdit = new PermissionPK(new Integer(PermissionBean.ORGANISATION_EDIT));
    private PermissionPK permissionDepartmentCreate = new PermissionPK(new Integer(PermissionBean.DEPARTMENT_CREATE));
    private PermissionPK permissionDepartmentEdit = new PermissionPK(new Integer(PermissionBean.DEPARTMENT_EDIT));
    private PermissionPK permissionPersonCreate = new PermissionPK(new Integer(PermissionBean.PERSON_CREATE));
    private PermissionPK permissionPersonEdit = new PermissionPK(new Integer(PermissionBean.PERSON_EDIT));
    private PermissionPK permissionUserCreate = new PermissionPK(new Integer(PermissionBean.USER_CREATE));
    private PermissionPK permissionUserEdit = new PermissionPK(new Integer(PermissionBean.USER_EDIT));
    private PermissionPK permissionUserEnable = new PermissionPK(new Integer(PermissionBean.USER_ENABLE));
    private PermissionPK permissionGroupCreate = new PermissionPK(new Integer(PermissionBean.GROUP_CREATE));
    private PermissionPK permissionGroupEdit = new PermissionPK(new Integer(PermissionBean.GROUP_EDIT));
    private PermissionPK permissionTaskTypeCreate = new PermissionPK(new Integer(PermissionBean.TASK_TYPE_CREATE));
    private PermissionPK permissionTaskTypeEdit = new PermissionPK(new Integer(PermissionBean.TASK_TYPE_EDIT));
    private PermissionPK permissionStateCreate = new PermissionPK(new Integer(PermissionBean.STATE_CREATE));
    private PermissionPK permissionStateEdit = new PermissionPK(new Integer(PermissionBean.STATE_EDIT));
    private PermissionPK permissionStatusCreate = new PermissionPK(new Integer(PermissionBean.STATUS_CREATE));
    private PermissionPK permissionStatusEdit = new PermissionPK(new Integer(PermissionBean.STATUS_EDIT));
    private PermissionPK permissionRoleCreate = new PermissionPK(new Integer(PermissionBean.ROLE_CREATE));
    private PermissionPK permissionRoleEdit = new PermissionPK(new Integer(PermissionBean.ROLE_EDIT));
    private PermissionPK permissionSprintCreate = new PermissionPK(new Integer(PermissionBean.SPRINT_CREATE));
    private PermissionPK permissionSprintEdit = new PermissionPK(new Integer(PermissionBean.SPRINT_EDIT));
    private PermissionPK permissionUserAccessCreate = new PermissionPK(new Integer(PermissionBean.USER_ACCESS_CREATE));
    private PermissionPK permissionGroupAccessCreate = new PermissionPK(new Integer(PermissionBean.GROUP_ACCESS_CREATE));
    private PermissionPK permissionTimeCreate = new PermissionPK(new Integer(PermissionBean.TIME_CREATE));
    private PermissionPK permissionSystemPropertyEdit = new PermissionPK(new Integer(PermissionBean.SYSTEM_PROPERTY_EDIT));
    private PermissionPK permissionContactDetailTypeEdit = new PermissionPK(new Integer(PermissionBean.CONTACT_DETAIL_TYPE_EDIT));
    private PermissionPK permissionTaskReassignResponsibility = new PermissionPK(new Integer(PermissionBean.TASK_REASSIGN_RESPONSIBILITY));
    
    
    /** Creates a new instance of NodeUtil */
    private NodeUtil() {
        m_nodeTabs = new Hashtable();
        m_nodeTabs.put("Node", new String[]{"Node", "Links", "Change Log", "User Access", "Group Access"});
        m_nodeTabs.put("Sprint", new String[]{"Sprint", "Links", "Change Log", "BurnDown", "Person Time", "Time Report", "Unplanned", "User Access", "Group Access"});
        m_nodeTabs.put("Department", new String[]{"Department", "Links", "Change Log", "User Access", "Group Access"});
        m_nodeTabs.put("Organisation", new String[]{"Organisation", "Links", "Change Log", "User Access", "Group Access"});
        m_nodeTabs.put("Person", new String[]{"Person", "Links", "Change Log", "Person Time", "User Access", "Group Access"});
        m_nodeTabs.put("Group", new String[]{"Group", "Links", "Change Log", "My Access", "User Access", "Group Access"});
        m_nodeTabs.put("Role", new String[]{"Role", "Links", "Change Log", "User Access", "Group Access"});
        m_nodeTabs.put("State", new String[]{"State", "Links", "Change Log", "User Access", "Group Access"});
        m_nodeTabs.put("Status", new String[]{"Status", "Links", "Change Log", "User Access", "Group Access"});
        m_nodeTabs.put("Action", new String[]{"Action", "Links", "Change Log", "Time", "Gantt", "BurnDown", "User Access", "Group Access"});
        m_nodeTabs.put("Anchor", new String[]{"Anchor", "Links", "Change Log", "Gantt", "User Access", "Group Access"});
        m_nodeTabs.put("Issue", new String[]{"Issue", "Links", "Change Log", "Time", "Gantt", "BurnDown", "User Access", "Group Access"});
        m_nodeTabs.put("Project", new String[]{"Project", "Links", "Change Log", "Time", "Gantt", "BurnDown", "User Access", "Group Access"});
        m_nodeTabs.put("TaskStateMachine", new String[]{"TaskStateMachine", "Links", "Change Log", "User Access", "Group Access"});
        m_nodeTabs.put("User", new String[]{"User", "Links", "Change Log", "Logins", "My Access", "User Access", "Group Access"});
        m_nodeTabs.put("SystemNode", new String[]{"SystemNode", "Change Log", "User Access", "Group Access"});
        m_nodeTabs.put("ContactDetailTypes", new String[]{"ContactDetailTypes", "User Access", "Group Access"});
        m_nodeTabs.put("SystemProperties", new String[]{"SystemProperties", "User Access", "Group Access"});
        m_nodeTabs.put("LicenceManagement", new String[]{"LicenceManagement", "User Access", "Group Access"});
        m_nodeTabs.put("LoginHistory", new String[]{"LoginHistory", "User Access", "Group Access"});
        m_nodeTabs.put("Directory", new String[]{"Directory", "User Access", "Group Access"});
        m_months = new IntegerString[12];
        m_months[0] = new IntegerString(new Integer(0), "Jan");
        m_months[1] = new IntegerString(new Integer(1), "Feb");
        m_months[2] = new IntegerString(new Integer(2), "Mar");
        m_months[3] = new IntegerString(new Integer(3), "Apr");
        m_months[4] = new IntegerString(new Integer(4), "May");
        m_months[5] = new IntegerString(new Integer(5), "Jun");
        m_months[6] = new IntegerString(new Integer(6), "Jul");
        m_months[7] = new IntegerString(new Integer(7), "Aug");
        m_months[8] = new IntegerString(new Integer(8), "Sep");
        m_months[9] = new IntegerString(new Integer(9), "Oct");
        m_months[10] = new IntegerString(new Integer(10), "Nov");
        m_months[11] = new IntegerString(new Integer(11), "Dec");
        m_days = new IntegerString[31];
        for (int i = 0; i < m_days.length; i++)
            m_days[i] = new IntegerString(new Integer(i+1), Integer.toString(i+1));
        m_hours = new IntegerString[24];
        for (int i = 0; i < m_hours.length; i++)
            m_hours[i] = new IntegerString(new Integer(i), Integer.toString(i));
        m_minutes = new IntegerString[12];
        for (int i = 0; i < m_minutes.length; i++)
            m_minutes[i] = new IntegerString(new Integer(i*5), Integer.toString(i*5));
        m_durationDays = new IntegerString[30];
        for (int i = 0; i < m_durationDays.length; i++)
            m_durationDays[i] = new IntegerString(new Integer(i), Integer.toString(i));
        m_durationHours = new IntegerString[8];
        for (int i = 0; i < m_durationHours.length; i++)
            m_durationHours[i] = new IntegerString(new Integer(i), Integer.toString(i));
    }
    
    public static NodeUtil getInstance() {
        if (m_cachedNodeUtil == null)
            m_cachedNodeUtil = new NodeUtil();
        return m_cachedNodeUtil;
    }
    
    public String[] getNodeTabs(String nodeType) {
        return (String[])m_nodeTabs.get(nodeType);
    }
    
    public IntegerString[] getYears() {
        int year = Calendar.getInstance().get(Calendar.YEAR);
        int start = year - 4;
        int stop = year + 4;
        IntegerString[] years = new IntegerString[stop-start+1];
        for (int i = start; i <= stop; i++) {
            years[i-start] = new IntegerString(new Integer(i), Integer.toString(i));
        }
        return years;
    }
    
    public IntegerString[] getMonths() {
        return m_months;
    }
    
    public IntegerString[] getDays() {
        return m_days;
    }
    
    public IntegerString[] getHours() {
        return m_hours;
    }
    
    public IntegerString[] getMinutes() {
        return m_minutes;
    }
    
    public IntegerString[] getDurationDays() {
        return m_durationDays;
    }
    
    public IntegerString[] getDurationHours() {
        return m_durationHours;
    }
    
    public MenuDTO getMenu(KeySession key, NodeValue nodeValue, Set permissionPKs, boolean isValidPasteNode, boolean isValidLinkNode, boolean isValidDependencyNode, boolean isValidDependantNode) {
        MenuDTO menuDTO = new MenuDTO();
        // create file menu
        List fileList = new ArrayList();
        fileList.add(new ButtonValue(true, true, "file-home", "Home"));
        fileList.add(new ButtonValue(true, true, "file-logout", "Logout"));
        menuDTO.addMenu("File", fileList);
        if (nodeValue != null) {
            // create edit menu
            List editList = new ArrayList();
            editList.add(new ButtonValue(!nodeValue.getFinalType().equals("SystemNode"), false, "edit-node-mark", "Mark Node"));
            editList.add(new ButtonValue(isValidPasteNode, false, "edit-node-move", "Move Marked Node"));
            editList.add(new ButtonValue(permissionPKs.contains(permissionNodeDelete), false, "edit-node-delete", "Delete Node"));
            editList.add(new ButtonValue(isValidLinkNode, false, "edit-node-link", "Link Marked Node"));
            editList.add(new ButtonValue(isValidDependencyNode, false, "edit-node-dependency", "Mark Dependency"));
            editList.add(new ButtonValue(isValidDependantNode, false, "edit-node-dependant", "Mark Dependant"));
            menuDTO.addMenu("Edit", editList);
            // create Create menu
            List createList = new ArrayList();
            createList.add(new ButtonValue(permissionPKs.contains(permissionNodeCreate), true, "create-node", "Node"));
            createList.add(new ButtonValue(permissionPKs.contains(permissionSprintCreate), true, "create-sprint", "Sprint"));
            createList.add(new ButtonValue(permissionPKs.contains(permissionProjectCreate), true, "create-project", "NewProject"));
            createList.add(new ButtonValue(permissionPKs.contains(permissionAnchorCreate), true, "create-anchor", "Anchor"));
            createList.add(new ButtonValue(permissionPKs.contains(permissionActionCreate), true, "create-action", "Action"));
            createList.add(new ButtonValue(permissionPKs.contains(permissionIssueCreate), true, "create-issue", "Issue"));
            createList.add(new ButtonValue(nodeValue.getName().equals("Setup Organisations") && permissionPKs.contains(permissionOrganisationCreate), true, "create-organisation", "Organisation"));
            createList.add(new ButtonValue((nodeValue.getFinalType().equals("Organisation") || nodeValue.getFinalType().equals("Department")) && permissionPKs.contains(permissionDepartmentCreate), true, "create-department", "Department"));
            createList.add(new ButtonValue((nodeValue.getFinalType().equals("Organisation") || nodeValue.getFinalType().equals("Department")) && permissionPKs.contains(permissionPersonCreate), true, "create-person", "Person"));
            createList.add(new ButtonValue(nodeValue.getName().equals("Setup Users") && permissionPKs.contains(permissionUserCreate), true, "create-user", "User"));
            createList.add(new ButtonValue(nodeValue.getName().equals("Setup Groups") && permissionPKs.contains(permissionGroupCreate), true, "create-group", "Group"));
            createList.add(new ButtonValue(nodeValue.getName().equals("Setup Task Types") && permissionPKs.contains(permissionTaskTypeCreate), true, "create-task-type", "Task Type"));
            createList.add(new ButtonValue(nodeValue.getName().equals("Setup States") && permissionPKs.contains(permissionStateCreate), true, "create-state", "State"));
            createList.add(new ButtonValue(nodeValue.getName().equals("Setup Statuses") && permissionPKs.contains(permissionStatusCreate), true, "create-status", "Status"));
            createList.add(new ButtonValue(nodeValue.getName().equals("Setup Roles") && permissionPKs.contains(permissionRoleCreate), true, "create-role", "Role"));
            createList.add(new ButtonValue(permissionPKs.contains(permissionUserAccessCreate), true, "create-user-access", "User Access"));
            createList.add(new ButtonValue(permissionPKs.contains(permissionGroupAccessCreate), true, "create-group-access", "Group Access"));
            menuDTO.addMenu("Create", createList);
        }
        // create search menu
        List searchList = new ArrayList();
        searchList.add(new ButtonValue(true, false, "search-tasks-all-responsible", "All My Responsible Tasks"));
        searchList.add(new ButtonValue(true, false, "search-tasks-all-interested", "All My Interested Tasks"));
        if (nodeValue != null) {
            searchList.add(new ButtonValue(true, false, "search-tasks-my-responsible", "My Responsible Tasks"));
            searchList.add(new ButtonValue(true, false, "search-tasks-my-interested", "My Interested Tasks"));
            searchList.add(new ButtonValue(true, false, "search-tasks-others-responsible", "Others Responsible Tasks"));
            searchList.add(new ButtonValue(true, false, "search-tasks-new", "New Tasks"));
            searchList.add(new ButtonValue(true, false, "search-tasks-active", "Active Tasks"));
            searchList.add(new ButtonValue(true, false, "search-tasks-completed", "Completed Tasks"));
            searchList.add(new ButtonValue(true, false, "search-tasks-changed", "Changed Tasks"));
            searchList.add(new ButtonValue(true, false, "search-tasks-advanced", "Advanced Search"));
            searchList.add(new ButtonValue(true, false, "search-time", "Time Report"));
        }
        menuDTO.addMenu("Search", searchList);
        // create Bookmark menu
        List bookmarkList = new ArrayList();
        if (nodeValue != null) {
            bookmarkList.add(new ButtonValue(true, false, "bookmark-add-node", "Add Bookmark"));
            bookmarkList.add(new ButtonValue(true, false, "bookmark-remove-node", "Remove Bookmark"));
        }
        try {
            if (key.getShowBookmarks() == false)
                bookmarkList.add(new ButtonValue(true, false, "bookmark-show", "Show Bookmarks"));
            else
                bookmarkList.add(new ButtonValue(true, false, "bookmark-hide", "Hide Bookmarks"));
        } catch(RemoteException e) {
        }
        bookmarkList.add(new ButtonValue(false, false, "", ""));
        try {
            RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
            Iterator i = cs.getBookmarkNodeValues(key).iterator();
            while(i.hasNext()) {
                NodeValue bookmarkNodeValue = (NodeValue)i.next();
                bookmarkList.add(new ButtonValue(true, false, "bookmark-nodeId-" + bookmarkNodeValue.getNodeId(), bookmarkNodeValue.getName()));
            }
        }
        catch(NamingException e) {
        }
        catch(CreateException e) {
        }
        catch(RemoteException e) {
        }
        catch(InvalidKeyException e) {
        }
        menuDTO.addMenu("Bookmarks", bookmarkList);
        // create Help menu
        List helpList = new ArrayList();
        helpList.add(new ButtonValue(true, false, "help-content", "Help Content"));
        helpList.add(new ButtonValue(true, false, "help-about", "Help About"));
        menuDTO.addMenu("Help", helpList);
        return menuDTO;
    }
    
    public List getNodeButtons(NodeValue nodeValue, String tab, Set permissionPKs) {
        List buttonList = new ArrayList();
        if (nodeValue.getType().equals("Contactable")) {
            String contactableType = nodeValue.getFinalType();
            if (contactableType.equals("Department")) {
                if (tab.equals("Department")) {
                    buttonList.add(new ButtonValue(permissionPKs.contains(permissionDepartmentEdit), true, "EditDepartment", "Edit"));
                }
            }
            if (contactableType.equals("Organisation")) {
                if (tab.equals("Organisation")) {
                    buttonList.add(new ButtonValue(permissionPKs.contains(permissionOrganisationEdit), true, "EditOrganisation", "Edit"));
                }
            }
            if (contactableType.equals("Person")) {
                if (tab.equals("Person")) {
                    buttonList.add(new ButtonValue(permissionPKs.contains(permissionPersonEdit), true, "EditPerson", "Edit"));
                }
            }
        }
        if (nodeValue.getType().equals("Group")) {
            if (tab.equals("Group")) {
                buttonList.add(new ButtonValue(permissionPKs.contains(permissionGroupEdit), true, "EditGroup", "Edit"));
            }
        }
        if (nodeValue.getType().equals("Role")) {
            if (tab.equals("Role")) {
                buttonList.add(new ButtonValue(permissionPKs.contains(permissionRoleEdit), true, "EditRole", "Edit"));
            }
        }
        if (nodeValue.getType().equals("Sprint")) {
            if (tab.equals("Sprint")) {
                buttonList.add(new ButtonValue(permissionPKs.contains(permissionSprintEdit), true, "EditSprint", "Edit"));
            }
        }
        if (nodeValue.getType().equals("State")) {
            if (tab.equals("State")) {
                buttonList.add(new ButtonValue(permissionPKs.contains(permissionStateEdit), true, "EditState", "Edit"));
            }
        }
        if (nodeValue.getType().equals("Status")) {
            if (tab.equals("Status")) {
                buttonList.add(new ButtonValue(permissionPKs.contains(permissionStatusEdit), true, "EditStatus", "Edit"));
            }
        }
        if (nodeValue.getType().equals("Task")) {
            String taskType = nodeValue.getFinalType();
            if (taskType.equals("Action")) {
                if (tab.equals("Action")) {
                    buttonList.add(new ButtonValue(permissionPKs.contains(permissionActionEdit), true, "EditAction", "Edit"));
                    buttonList.add(new ButtonValue(permissionPKs.contains(permissionNodePostComment), true, "Comment", "Post Comment"));
                    buttonList.add(new ButtonValue(permissionPKs.contains(permissionTaskChangeState), true, "ChangeState", "Change State"));
                    buttonList.add(new ButtonValue(permissionPKs.contains(permissionTaskReassignResponsibility), true, "ReassignResponsibility", "Reassign"));
                    buttonList.add(new ButtonValue(true, true, "ToggleUserInterest", "Toggle Interest"));
                }
                if (tab.equals("Time") || tab.equals("Action")) {
                    buttonList.add(new ButtonValue(permissionPKs.contains(permissionTimeCreate), true, "CreateTime", "Create Time"));
                }
            }
            if (taskType.equals("Anchor")) {
                if (tab.equals("Anchor")) {
                    buttonList.add(new ButtonValue(permissionPKs.contains(permissionAnchorEdit), true, "EditAnchor", "Edit"));
                    buttonList.add(new ButtonValue(permissionPKs.contains(permissionNodePostComment), true, "Comment", "Post Comment"));
                    buttonList.add(new ButtonValue(true, true, "ToggleUserInterest", "Toggle Interest"));
                }
            }
            if (taskType.equals("Issue")) {
                if (tab.equals("Issue")) {
                    buttonList.add(new ButtonValue(permissionPKs.contains(permissionIssueEdit), true, "EditIssue", "Edit"));
                    buttonList.add(new ButtonValue(permissionPKs.contains(permissionNodePostComment), true, "Comment", "Post Comment"));
                    buttonList.add(new ButtonValue(permissionPKs.contains(permissionTaskChangeState), true, "ChangeState", "Change State"));
                    buttonList.add(new ButtonValue(permissionPKs.contains(permissionTaskReassignResponsibility), true, "ReassignResponsibility", "Reassign"));
                    buttonList.add(new ButtonValue(true, true, "ToggleUserInterest", "Toggle Interest"));
                }
                if (tab.equals("Time") || tab.equals("Issue")) {
                    buttonList.add(new ButtonValue(permissionPKs.contains(permissionTimeCreate), true, "CreateTime", "Create Time"));
                }
           }
            if (taskType.equals("Project")) {
                if (tab.equals("Project")) {
                    buttonList.add(new ButtonValue(permissionPKs.contains(permissionProjectEdit), true, "EditProject", "Edit"));
                    buttonList.add(new ButtonValue(permissionPKs.contains(permissionNodePostComment), true, "Comment", "Post Comment"));
                    buttonList.add(new ButtonValue(permissionPKs.contains(permissionTaskChangeState), true, "ChangeState", "Change State"));
                    buttonList.add(new ButtonValue(permissionPKs.contains(permissionTaskReassignResponsibility), true, "ReassignResponsibility", "Reassign"));
                    buttonList.add(new ButtonValue(true, true, "ToggleUserInterest", "Toggle Interest"));
                    buttonList.add(new ButtonValue(permissionPKs.contains(permissionProjectEdit), true, "UpdateTime", "Update Time"));
                }
                if (tab.equals("Time") || tab.equals("Project")) {
                    buttonList.add(new ButtonValue(permissionPKs.contains(permissionTimeCreate), true, "CreateTime", "Create Time"));
                }
            }
        }
        if (nodeValue.getType().equals("TaskStateMachine")) {
            if (tab.equals("TaskStateMachine")) {
                buttonList.add(new ButtonValue(permissionPKs.contains(permissionTaskTypeEdit), true, "EditTaskStateMachine", "Edit"));
                buttonList.add(new ButtonValue(permissionPKs.contains(permissionTaskTypeEdit), true, "StateMachine", "State Machine"));
            }
        }
        if (nodeValue.getType().equals("User")) {
            if (tab.equals("User")) {
                buttonList.add(new ButtonValue(permissionPKs.contains(permissionUserEdit), true, "EditUser", "Edit"));
                buttonList.add(new ButtonValue(true, true, "Password", "Password"));
                buttonList.add(new ButtonValue(permissionPKs.contains(permissionUserEnable), true, "ToggleUserEnabled", "Toggle Enabled"));
            }
        }
        if (nodeValue.getType().equals("Node")) {
            String nodeType = nodeValue.getFinalType();
            if (nodeType.equals("SystemNode")) {
            }
            if (nodeType.equals("ContactDetailTypes")) {
                if (tab.equals("ContactDetailTypes")) {
                    buttonList.add(new ButtonValue(permissionPKs.contains(permissionContactDetailTypeEdit), true, "EditContactDetailType", "Edit"));
                }
            }
            if (nodeType.equals("SystemProperties")) {
                if (tab.equals("SystemProperties")) {
                    buttonList.add(new ButtonValue(permissionPKs.contains(permissionSystemPropertyEdit), true, "EditSystemProperty", "Edit"));
                }
            }
            if (nodeType.equals("LicenceManagement")) {
                if (tab.equals("LicenceManagement")) {
                    buttonList.add(new ButtonValue(true, true, "AddLicence", "Add Licence"));
                }
            }
            if (nodeType.equals("Node")) {
                if (tab.equals("Node")) {
                    buttonList.add(new ButtonValue(permissionPKs.contains(permissionNodeEdit), true, "EditNode", "Edit"));
                }
            }
        }
        return buttonList;
    }
    
    static public Collection getAllRolePairs(KeySession p_key, RemoteClientSession p_cs) throws InvalidKeyException, RemoteException {
        Collection roleIntegerStrings = new ArrayList();
        RoleValue[] roleValues = p_cs.getAllRoleValues(p_key);
        for (int i = 0; i < roleValues.length; i++) {
            roleIntegerStrings.add(new IntegerString(roleValues[i].getRoleId(), roleValues[i].toString()));
        }
        return roleIntegerStrings;
    }
    
    static public Collection getAllTaskTypePairs(KeySession p_key, RemoteClientSession p_cs) throws InvalidKeyException, RemoteException {
        Collection taskTypeIntegerStrings = new ArrayList();
        Iterator i = p_cs.getAllTaskStateMachineValues(p_key).iterator();
        while(i.hasNext()) {
            TaskStateMachineValue taskStateMachineValue = (TaskStateMachineValue)i.next();
            taskTypeIntegerStrings.add(new IntegerString(taskStateMachineValue.getTaskStateMachineId(), taskStateMachineValue.toString()));
        }
        return taskTypeIntegerStrings;
    }
    
    static public Collection getAllUserPairs(KeySession p_key, RemoteClientSession p_cs) throws InvalidKeyException, RemoteException {
        Collection userIntegerStrings = new ArrayList();
        Iterator i = p_cs.getAllUserValues(p_key).iterator();
        while(i.hasNext()) {
            UserValue userValue = (UserValue)i.next();
            userIntegerStrings.add(new IntegerString(userValue.getUserId(), userValue.toString()));
        }
        return userIntegerStrings;
    }
    
    static public Collection getAllPersonPairs(KeySession p_key, RemoteClientSession p_cs) throws InvalidKeyException, RemoteException {
        Collection personIntegerStrings = new ArrayList();
        Iterator i = p_cs.getAllPersonValues(p_key).iterator();
        while(i.hasNext()) {
            PersonValue personValue = (PersonValue)i.next();
            personIntegerStrings.add(new IntegerString(personValue.getPersonId(), personValue.toString()));
        }
        return personIntegerStrings;
    }
    
    static public Collection getAllGroupPairs(KeySession p_key, RemoteClientSession p_cs) throws InvalidKeyException, RemoteException {
        Collection groupIntegerStrings = new ArrayList();
        Iterator i = p_cs.getAllGroupValues(p_key).iterator();
        while(i.hasNext()) {
            GroupValue groupValue = (GroupValue)i.next();
            groupIntegerStrings.add(new IntegerString(groupValue.getGroupId(), groupValue.toString()));
        }
        return groupIntegerStrings;
    }
    
    static public Collection getAllDepartmentPairs(KeySession p_key, RemoteClientSession p_cs) throws InvalidKeyException, RemoteException {
        Collection departmentIntegerStrings = new ArrayList();
        Iterator i = p_cs.getAllDepartmentValues(p_key).iterator();
        while(i.hasNext()) {
            DepartmentValue departmentValue = (DepartmentValue)i.next();
            departmentIntegerStrings.add(new IntegerString(departmentValue.getDepartmentId(), departmentValue.getQualifiedName()));
        }
        return departmentIntegerStrings;
    }
    
    static public Collection getAllNotificationPairs(KeySession p_key, RemoteClientSession p_cs) throws InvalidKeyException, RemoteException {
        Collection notificationIntegerStrings = new ArrayList();
        NotificationMechanismValue[] notificationValues = p_cs.getAllNotificationMechanismValues(p_key);
        for (int i = 0; i < notificationValues.length; i++) {
            notificationIntegerStrings.add(new IntegerString(notificationValues[i].getNotificationMechanismId(), notificationValues[i].getName()));
        }
        return notificationIntegerStrings;
    }
    
    static public Collection getAllStatePairs(KeySession p_key, RemoteClientSession p_cs) throws InvalidKeyException, RemoteException {
        Collection stateIntegerStrings = new ArrayList();
        StateValue[] stateValues = p_cs.getAllStateValues(p_key);
        for (int i = 0; i < stateValues.length; i++) {
            stateIntegerStrings.add(new IntegerString(stateValues[i].getStateId(), stateValues[i].getNodeValue().getName()));
        }
        return stateIntegerStrings;
    }
    
    static public Collection getAllButStartStatePairs(KeySession p_key, RemoteClientSession p_cs) throws InvalidKeyException, RemoteException {
        Collection stateIntegerStrings = new ArrayList();
        StateValue[] stateValues = p_cs.getAllStateValues(p_key);
        StateValue startStateValue = p_cs.getStartStateValue(p_key);
        for (int i = 0; i < stateValues.length; i++) {
            if (!stateValues[i].equals(startStateValue))
                stateIntegerStrings.add(new IntegerString(stateValues[i].getStateId(), stateValues[i].getNodeValue().getName()));
        }
        return stateIntegerStrings;
    }
    
    static public Collection getAllStatusPairs(KeySession p_key, RemoteClientSession p_cs) throws InvalidKeyException, RemoteException {
        Collection statusIntegerStrings = new ArrayList();
        StatusValue[] statusValues = p_cs.getAllStatusValues(p_key);
        for (int i = 0; i < statusValues.length; i++) {
            statusIntegerStrings.add(new IntegerString(statusValues[i].getStatusId(), statusValues[i].getNodeValue().getName()));
        }
        return statusIntegerStrings;
    }
    
    static public Collection getAllContactablePairs(KeySession p_key, RemoteClientSession p_cs) throws InvalidKeyException, RemoteException {
        Collection contactableIntegerStrings = new ArrayList();
        TreeModel contactableTreeModel = p_cs.getContactableValueTree(p_key);
        recursiveAddContactable(contactableTreeModel, contactableTreeModel.getRoot(), contactableIntegerStrings, 0);
        return contactableIntegerStrings;
    }

    public static void recursiveAddContactable(TreeModel p_contactableTreeModel, Object p_node, Collection p_contactableList, int p_level) {
        Object userObject = ((DefaultMutableTreeNode)p_node).getUserObject();
        if (userObject instanceof ContactableValue) {
            StringBuffer buffer = new StringBuffer();
            for (int i = 0; i < p_level-1; i++)
                buffer.append(" - ");
            ContactableValue contactableValue = (ContactableValue)userObject;
            buffer.append(contactableValue.toString());
            p_contactableList.add(new IntegerString(contactableValue.getContactableId(), buffer.toString()));
        }
        for (int i = 0; i < p_contactableTreeModel.getChildCount(p_node); i++) {
            Object nextNode = p_contactableTreeModel.getChild(p_node, i);
            recursiveAddContactable(p_contactableTreeModel, nextNode, p_contactableList, p_level+1);
        }
    }
    
}
